package com.naiyumie.ncalculator;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.naiyumie.stastics.StasticsBasic;

public class PercentMultiple02Activity extends KingWangJjangActivity implements OnClickListener {
	
	String msg = null;
	EditText etf1 = null, etf2 = null;
	TextView tve1 = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_percent_multiple_02);
		Intent fromIntent = getIntent();
		msg = fromIntent.getStringExtra("MSG");
		this.setMsg(msg);
		this.callMethod();
		tvTitle.setText(R.string.category03);
		etf1 = (EditText) findViewById(R.id.etf1);
		etf1.setFilters(new InputFilter[] { nWidgetTool.regEditTextFilter("^[0-9]+$") });
		etf1.addTextChangedListener(percentMultipleWatcher);
		etf2 = (EditText) findViewById(R.id.etf2);
		etf2.addTextChangedListener(percentMultipleWatcher);
		etf2.setFilters(new InputFilter[] { nWidgetTool.regEditTextFilter("^[,0-9]+$") });
		tve1 = (TextView) findViewById(R.id.tvHelperText);
	}
	
	private final TextWatcher percentMultipleWatcher = new TextWatcher() {
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		public void afterTextChanged(Editable s) {
			String totalValue = etf1.getText().toString();
			String inputValue = etf2.getText().toString();
			if (!Pattern.matches("^[0-9]+$", totalValue)) {
				nlog.puts("invalid totalValue");
				return;
			}
			if (!Pattern.matches("^[,0-9]+$", inputValue)) {
				nlog.puts("invalid inputValue");
				return;
			}
			String result = "";
			BigDecimal totalPercent = BigDecimal.ZERO;
			BigDecimal totalValueBigDecimal = BigDecimal.ZERO;
			try {
				String regExp = ",";
				String[] inputValueArray = inputValue.split(regExp);
				totalValueBigDecimal = new BigDecimal(totalValue);
				result += "[입력한 퍼센트값과 도출된 각각의 값] \n";
				for (int i = 0; i < inputValueArray.length; i++) {
					// (전체값 * 퍼센트) / 100
					BigDecimal percenteValue = new BigDecimal(inputValueArray[i]);
					BigDecimal calculate = totalValueBigDecimal.multiply(percenteValue);
					BigDecimal calculateResult = calculate.divide(new BigDecimal(100), 0, BigDecimal.ROUND_UP); // 갯수
					totalPercent = totalPercent.add(percenteValue);
					result += String.format("%d : 전체값 %s의 %s%%는 %s 입니다.\n", i + 1, totalValueBigDecimal.toString(), inputValueArray[i].toString(), String.valueOf(calculateResult));
				}
			} catch (Exception e) {
				nlog.puts("error");
			} finally {
				String remainPercent = new BigDecimal("100").subtract(totalPercent).toString();
				result += "\n";
				result += String.format("입력 퍼센트의 총합 : %s%%, 백퍼센트까지 : %s%%\n", totalPercent.toString(), remainPercent);
				tve1.setText(result);
			}
		}
	};
}
